package cn.kpi.service.workassessment.impl;

import cn.kpi.dao.workassessment.WorkAssTemplateDao;
import cn.kpi.dao.workassessment.WorkKPIDao;
import cn.kpi.entity.workassessment.*;
import cn.kpi.service.workassessment.IWorkAssTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;
import java.util.Map;

/**
 * 法律绩效模板服务
 *
 * @author lex
 * @create 2018-06-17-21:33
 */
@Service
public class WorkAssTemplateService implements IWorkAssTemplateService {

    @Autowired
    private WorkAssTemplateDao workAssTemplateDao;


    @Autowired
    private WorkKPIDao workKPIDao;

    public List<WorkAssTemplateIndexEntity> GetTemplateIndexListRowSpan() throws Exception {
        try {
            return workAssTemplateDao.GetTemplateIndexListRowSpan();

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    public List<WorkAssTemplateEntity> GetWorkAssTemplateListByWhere(String where) throws Exception {
        try {
            return workAssTemplateDao.GetWorkAssTemplateListByWhere(where);

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    public int GetWorkAssTemplateListCountByWhere(String where) throws Exception {
        try {
            return workAssTemplateDao.GetWorkAssTemplateListCountByWhere(where);

        } catch (Exception ex) {
            ex.printStackTrace();
            return -1;
        }
    }

    @Override
    public int UpdateStatus(Long templateid) {
        return workAssTemplateDao.UpdateStatus(templateid);
    }

    @Override
    public List<WorkAssTemplateIndexEntity> GetTemplateIndexListByWhere(String where) throws Exception {
        try {
            return workAssTemplateDao.GetTemplateIndexListByWhere(where);

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    public WorkAssTemplateIndexEntity GetIndexById(Long assindexid) throws Exception {
        try {
            return workAssTemplateDao.GetIndexById(assindexid);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public List<WorkAssTempCreateIndexEntity> GetIndexListByWhere(String where) throws Exception {
        try {
            return workAssTemplateDao.GetIndexListByWhere(where);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public int UpdateIndex(WorkAssTemplateIndexEntity entity) throws Exception {
        try {
            return workAssTemplateDao.UpdateIndex(entity);
        } catch (Exception ex) {
            ex.printStackTrace();
            return -1;
        }
    }

    @Transactional
    public int InsertTemplateIndexentity(WorkAssTemplateIndexEntity entity) throws Exception {
        try {
            int result = 0;
            if (entity.getParentid() > 0) {
                result += workAssTemplateDao.UpdateParentscore(entity.getParentid(), entity.getScore());
            }
            result += workAssTemplateDao.InsertTemplateIndexentity(entity);
            return result;

        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return -1;
        }
    }

    @Transactional
    public int DeleteIndex(Long assindexid, Long parentid) throws Exception {
        try {
            int result = 0;
            int score = GetIndexById(assindexid).getScore();
            result += workAssTemplateDao.UpdateParentscore(parentid, (0 - score));
            result += workAssTemplateDao.DeleteIndex(assindexid, parentid);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return -1;
        }
    }

    @Transactional
    public int DeleteWorkTempEntity(Long templateid) throws Exception {
        try {
            int result = workAssTemplateDao.DeleteWorkTempCreateIndexEntity(templateid);
            result += workAssTemplateDao.DeleteWorkTempEntity(templateid);
            result += workAssTemplateDao.DeleteWorkTempOrgBatch(templateid);
            if (result <= 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物
                return -1;
            }
            return result;

        } catch (Exception ex) {
            ex.printStackTrace();
            return -1;
        }
    }

    public WorkAssTemplateEntity GetWorkAssTemplateListById(Long templateid) throws Exception {
        try {
            return workAssTemplateDao.GetWorkAssTemplateListById(templateid);

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    public List<WorkAssTemplateOrgEntity> GetTempOrgListByTempId(Long templateid) {
        try {
            return workAssTemplateDao.GetTempOrgListByTempId(templateid);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    public int InsertWorkTempOrgBatch(Long itemids, List<Long> orgids) throws Exception {
        try {
            workAssTemplateDao.DeleteWorkTempOrgBatch(itemids);
            int result = workAssTemplateDao.InsertWorkTempOrgBatch(itemids, orgids);
            if (result <= 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物
                return -1;
            }
            return result;

        } catch (Exception ex) {
            ex.printStackTrace();
            return -1;
        }
    }

    @Override
    public int DeleteWorkTempOrgBatch(Long templateid) {
        try {
            int result = 0;
            int score = workAssTemplateDao.DeleteWorkTempOrgBatch(templateid);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return -1;
        }
    }

    @Override
    public List<WorkAssTempCreateIndexEntity> GetTemplateIndexBytemplateid(Long templateid) throws Exception {
        try {
            return workAssTemplateDao.GetTemplateIndexBytemplateid(templateid);

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Map> getSumScore(Long templateid) {
        return workAssTemplateDao.getSumScore(templateid);
    }

    @Override
    public List<WorkAssTemplateOrgEntity> GetChooseOrgListByTempId(Long templateid) {
        try {
            return workAssTemplateDao.GetChooseOrgListByTempId(templateid);

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    public List<WorkAssGroupOrgEntity> GetGroupOrgListByTempId(Long templateid) {
        try {
            return workAssTemplateDao.GetGroupOrgListByTempId(templateid);

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Map> getAssindexS(Long templateid) {
        return workKPIDao.getAssindexS(templateid);
    }

    @Override
    public List<WorkAssTemplateEntity> getWorkAssTemplateEntitys(int year, int quarter) {
        try {
            return workKPIDao.getWorkAssTemplateEntitys(year, quarter);
        } catch (Exception ex) {

        }
        return null;
    }

    @Override
    public int updateTitlleName(Long templateid, String titleName) {
        return workAssTemplateDao.updateTitleName(templateid, titleName);
    }


    public List<WorkAssTemplateIndexEntity> GetTemplateIndexListRowSpanByTempId(Long templateid) throws Exception {
        try {
            return workAssTemplateDao.GetTemplateIndexListRowSpanByTempId(templateid);

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Transactional
    public int CreateTemplateAndIndex(List<WorkAssTempCreateIndexEntity> lst, WorkAssTemplateEntity entity) throws Exception {
        try {
            int result = workAssTemplateDao.InsertTemplateEntity(entity);
            Long tempid = entity.getTemplateid();
            for (int i = 0; i < lst.size(); i++) {
                lst.get(i).setTemplateid(tempid);
            }
            result += workAssTemplateDao.InsertTempCreateIndexBatch(lst);
            if (result <= 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物
                return -1;
            }
            return result;

        } catch (Exception ex) {
            ex.printStackTrace();
            return -1;
        }
    }

    @Transactional
    public int UpdateWorkAssTempEntity(List<WorkAssTempCreateIndexEntity> lst, WorkAssTemplateEntity entity) throws Exception {
        try {
            int result = workAssTemplateDao.UpdateWorkAssTempEntity(entity);
            result += workAssTemplateDao.DeleteWorkTempCreateIndexEntity(entity.getTemplateid());
            result += workAssTemplateDao.InsertTempCreateIndexBatch(lst);
            if (result <= 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物
                return -1;
            }
            return result;

        } catch (Exception ex) {
            ex.printStackTrace();
            return -1;
        }
    }
    //endregion
}
